//
//  YWCalendarView.swift
//  ShouChong
//
//  Created by 陈友文 on 2024/6/19.
//

import SwiftUI
import FSCalendar

struct YWCalendarView:UIViewRepresentable {
   
    
    @AppStorage("curren_main_color") var currentMainColor = "color_main_01"
    @Binding var showMoreCalendar:Bool
    
    var didSelect:(_:Date) -> Void
    
    func makeUIView(context: Context) -> FSCalendar {
        let calendar = FSCalendar()
        calendar.delegate = context.coordinator
        calendar.appearance.selectionColor = .systemRed
        calendar.today = Date()
        calendar.locale = Locale(identifier: Locale.current.identifier)
//        calendar.placeholderType = .none
        calendar.setScope(.week, animated: false)
        calendar.select(Date())
        calendar.appearance.headerTitleColor = UIColor(named: "color_text_black")
        calendar.appearance.weekdayTextColor = UIColor(named: "color_text_black")
        calendar.appearance.titleSelectionColor = UIColor(named: "color_text_white")
        calendar.appearance.subtitleSelectionColor = UIColor(named: currentMainColor)
        calendar.appearance.selectionColor = UIColor(named: currentMainColor)
        calendar.appearance.titleFont = UIFont.systemFont(ofSize: 20, weight: .bold)
        calendar.appearance.headerMinimumDissolvedAlpha = 0
        calendar.appearance.todayColor = .gray
        
//        calendar.daysContainer.fs_height = 100
//        calendar.appearance.headerTitleOffset = CGPoint(x: 0, y: -20)
//        calendar.reloadData()
        return calendar
    }
    
    func updateUIView(_ uiView: FSCalendar, context: Context) {
        if self.showMoreCalendar {
            uiView.setScope(.month, animated: false)
            uiView.appearance.headerTitleColor = .black
            uiView.appearance.titleFont = UIFont.systemFont(ofSize: 16, weight: .semibold)
            uiView.appearance.headerTitleOffset = CGPoint(x: 0, y: -5)
            uiView.appearance.selectionColor = UIColor(named: currentMainColor)
//            uiView.headerHeight = 20
        } else {
            uiView.setScope(.week, animated: false)
            uiView.appearance.headerTitleColor = .clear
            uiView.appearance.titleFont = UIFont.systemFont(ofSize: 20, weight: .bold)
            uiView.appearance.headerTitleOffset = CGPoint(x: 0, y: 0)
            uiView.appearance.selectionColor = UIColor(named: currentMainColor)
//            uiView.headerHeight = 0

            
        }
        uiView.reloadData()
    }
    
    func makeCoordinator() -> Coordinator {
        .init(didSelect: didSelect)
    }
    
    
}

extension YWCalendarView{
    class Coordinator:NSObject,FSCalendarDelegate {
        var didSelect:(_:Date) -> Void
        init(didSelect: @escaping (_: Date) -> Void) {
            self.didSelect = didSelect
        }
        func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {
            didSelect(date)
        }
        
    
    }
    
}




